package comp353Games;

import javax.swing.*;
import javax.swing.BoxLayout;
import java.awt.*;

/**
 * Opener.java is an introductory GUI designed to allow the user to select a
 * game to play.
 *
 * @author Andre Harvey, Tyler Radley, and Deanna Fink
 * @version 4/30/12
 */
public class Opener extends JFrame {
    //Game sum button used in the game chooser
    private JToggleButton gameSumButton;
    
    //Toads and Frogs Button used in the game chooser
    private JButton toadsAndFrogsButton;
    
    //Nim Button used in the game chooser
    private JButton nimButton;
    
    //Toads and Frogs Button used in the game chooser
    private JButton snortButton;
    
    //Clobber Button used in the game chooser
    private JButton clobberButton;
    
    //Col Button used in the game chooser
    private JButton colButton;
    
    //Load Button used to load a game or sum of games
    private JButton loadButton;
    
    //Exit Button used in the game chooser
    private JButton exitButton;
    
    //The space for the welcome to the game suite
    private JLabel welcomeLabel;
    
    //Space for the prompt for the user to select a game or game sum
    private JLabel questionLabel;
    
    //The space for details about button usage when the cursor is hovered over
    private JLabel buttonDescriptionLabel;
    
    //The panel which the opener is displayed in
    private JPanel openerPanel;
    
    //Is true whenever the game sum button is activated
    private boolean isGameSum = false;
    
    //The number of games in the game sum
    private int numGames = 2;
    
    //The current index location of the game sum array
    private int index = 0;
    
    //An array of two games that are used to produce a game sum
    private GameModel[] gameSum = new GameModel[numGames];
    
    // the description of the buttons that are highlighted
    private String buttonDescription;
    
    //The messages that prompts the user to select a game or game sum
    private String[] openerMessage = new String[4];
    
    //The original texture of the buttons
    private Color originalColor;
    
    //Set to true if current game is SnortCol.  Used to differentiate between snort and col in the SnortCol game.
    private boolean isSnort = false;

    /**
     * Constructor which initializes the Opener.
     *
     */
    public Opener() {
        super("Awesome Game Program");
        initComponents();
    }

    /**
     * Initial components used to construct the Opener
     */
    private void initComponents() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        openerMessage[0] = "Which game would you like to play?";
        openerMessage[1] = "Game sum selected. Choose your first game (Left Side).";
        openerMessage[2] = "Game sum deselected. " + openerMessage[0];
        openerMessage[3] = "Choose your second game (Right Side).  Load a game now if desired..";
        openerPanel = new JPanel();
        openerPanel.setPreferredSize(new Dimension(500, 250));
        openerPanel.setLayout(new BoxLayout(openerPanel, BoxLayout.PAGE_AXIS));

        welcomeLabel = new JLabel();
        welcomeLabel.setFont(new java.awt.Font("Vijaya", 1, 24));
        welcomeLabel.setText("Welcome to the Game Suite");
        welcomeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        questionLabel = new JLabel();
        questionLabel.setFont(new java.awt.Font("Vijaya", 0, 18));
        questionLabel.setText("Which game would you like to play?");
        questionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        buttonDescriptionLabel = new JLabel();
        buttonDescriptionLabel.setFont(new java.awt.Font("Vijaya", 0, 16));
        buttonDescriptionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);


        loadButton = new JButton();
        loadButton.setText("Load");
        loadButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        loadButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                loadButtonActionPerformed(event);
            }
        });


        exitButton = new JButton();
        exitButton.setText("Exit");
        exitButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        exitButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                exitButtonActionPerformed(event);
            }
        });


        openerPanel.add(welcomeLabel);
        openerPanel.add(new Box.Filler(new Dimension(500, 30),
                new Dimension(500, 30),
                new Dimension(500, 30)));
        openerPanel.add(questionLabel);
        openerPanel.add(new Box.Filler(new Dimension(500, 50),
                new Dimension(500, 50),
                new Dimension(500, 50)));
        openerPanel.add(this.createButtonPane());
        openerPanel.add(new Box.Filler(new Dimension(500, 5),
                new Dimension(500, 5),
                new Dimension(500, 5)));
        openerPanel.add(loadButton);
        openerPanel.add(exitButton);
        openerPanel.add(new Box.Filler(new Dimension(500, 50),
                new Dimension(500, 50),
                new Dimension(500, 50)));
        openerPanel.add(buttonDescriptionLabel);
        openerPanel.add(new Box.Filler(new Dimension(500, 50),
                new Dimension(500, 50),
                new Dimension(500, 50)));

        this.add(openerPanel);

        pack();
        repaint();
    }

    /**
     * JPanel which holds the buttons to start the available games.
     *
     * @return the JPanel which holds the buttons to start the available games.
     */
    private JPanel createButtonPane() {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setAlignmentX(Component.CENTER_ALIGNMENT);

        gameSumButton = new JToggleButton();
        colButton = new JButton();
        toadsAndFrogsButton = new JButton();
        nimButton = new JButton();
        snortButton = new JButton();
        clobberButton = new JButton();

        gameSumButton.setText("Game Sum");
        gameSumButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                buttonDescription = "When Game Sum is on, you may select two games you wish to play simultaneously.";
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        gameSumButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                if (isGameSum) {
                    isGameSum = false;
                    questionLabel.setText(openerMessage[2]);
                    toadsAndFrogsButton.setBackground(originalColor);
                    nimButton.setBackground(originalColor);
                    snortButton.setBackground(originalColor);
                    clobberButton.setBackground(originalColor);
                } else {
                    isGameSum = true;
                    originalColor = nimButton.getBackground();
                    index = 0;
                    questionLabel.setText(openerMessage[1]);
                }
            }
        });

        colButton.setText("Col");
        colButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                if (isGameSum) {
                    buttonDescription = "Adds a new game of Col to the game sum.";
                } else {
                    buttonDescription = "Starts a new game of Col.";
                }
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        colButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                colButtonActionPerformed(event);
            }
        });

        toadsAndFrogsButton.setText("Toads And Frogs");
        toadsAndFrogsButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                if (isGameSum) {
                    buttonDescription = "Adds a new game of Toads and Frogs to the game sum.";
                } else {
                    buttonDescription = "Starts a new game of Toads and Frogs.";
                }
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        toadsAndFrogsButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                toadsAndFrogsButtonActionPerformed(event);
            }
        });

        nimButton.setText("Nim");
        nimButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                if (isGameSum) {
                    buttonDescription = "Adds a new game of Nim to the game sum.";
                } else {
                    buttonDescription = "Starts a new game of Nim.";
                }
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        nimButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                nimButtonActionPerformed(event);
            }
        });

        snortButton.setText("Snort");
        snortButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                if (isGameSum) {
                    buttonDescription = "Adds a new game of Snort to the game sum.";
                } else {
                    buttonDescription = "Starts a new game of Snort.";
                }
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        snortButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                snortButtonActionPerformed(event);
            }
        });

        clobberButton.setText("Clobber");
        clobberButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent event) {
                if (isGameSum) {
                    buttonDescription = "Adds a new game of Clobber to the game sum.";
                } else {
                    buttonDescription = "Starts a new game of Clobber.";
                }
                gameMouseEntered(event);
            }

            public void mouseExited(java.awt.event.MouseEvent event) {
                gameMouseExited(event);
            }
        });
        clobberButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                clobberButtonActionPerformed(event);
            }
        });

        buttonPane.add(gameSumButton);
        buttonPane.add(colButton);
        buttonPane.add(nimButton);
        buttonPane.add(toadsAndFrogsButton);
        buttonPane.add(snortButton);
        buttonPane.add(clobberButton);

        return buttonPane;
    }

    //Outputs the description of the game when the cursor hovers over its button.
    private void gameMouseEntered(java.awt.event.MouseEvent event) {
        buttonDescriptionLabel.setText(buttonDescription);
    }

    //Outputs instructions of how to proceed when the mouse stops highlighting a button.
    private void gameMouseExited(java.awt.event.MouseEvent event) {
        buttonDescriptionLabel.setText("");
    }

    //Executes the game sum.
    private void gameSumButtonActionPerformed(java.awt.event.ActionEvent event) {
        dispose();
        for (int i = 0; i < numGames - 1; i++) {
            new Thread(new Runnable() {

                public void run() {

                    GameController controller = new GameController(gameSum);
                    controller.GameController();
                }
            }).start();
        }
    }

    //Starts a new game of Toads and Frogs or adds it to the game sum.
    private void toadsAndFrogsButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            toadsAndFrogsButton.setBackground(new Color(184, 207, 229));
            if (index < numGames) {
                gameSum[index] = new ToadsAndFrogs();
                index++;
                if (index == numGames) {
                    gameSumButtonActionPerformed(event);
                } else {
                    questionLabel.setText(openerMessage[3]);
                }
            }
        } else {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    GameModel[] singleGame = new GameModel[]{new ToadsAndFrogs()};
                    GameController controller = new GameController(singleGame);
                    controller.GameController();
                }
            }).start();
        }
    }

    private void colButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            colButton.setBackground(new Color(184, 207, 229));
            if (index < numGames) {
                gameSum[index] = new SnortCol(false);
                index++;
                if (index == numGames) {
                    gameSumButtonActionPerformed(event);
                } else {
                    questionLabel.setText(openerMessage[3]);
                }
            }
        } else {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    GameModel[] singleGame = new GameModel[]{new SnortCol(false)};
                    GameController controller = new GameController(singleGame);
                    controller.GameController();
                }
            }).start();
        }
    }

    //Starts a new game of Nim or adds it to the game sum.
    private void nimButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            nimButton.setBackground(new Color(184, 207, 229));
            if (index < numGames) {
                gameSum[index] = new Nim();
                index++;
                if (index == numGames) {
                    gameSumButtonActionPerformed(event);
                } else {
                    questionLabel.setText(openerMessage[3]);
                }
            }
        } else {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    GameModel[] singleGame = new GameModel[]{new Nim()};
                    GameController controller = new GameController(singleGame);
                    controller.GameController();
                }
            }).start();
        }
    }

    //Starts a new game of SnortCol or adds it to the game sum.
    private void snortButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            snortButton.setBackground(new Color(184, 207, 229));
            if (index < numGames) {
                gameSum[index] = new SnortCol(true);
                index++;
                if (index == numGames) {
                    gameSumButtonActionPerformed(event);
                } else {
                    questionLabel.setText(openerMessage[3]);
                }
            }
        } else {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    GameModel[] singleGame = new GameModel[]{new SnortCol(true)};
                    GameController controller = new GameController(singleGame);
                    controller.GameController();
                }
            }).start();
        }
    }

    //Starts a new game of Clobber or adds it to the game sum.
    private void clobberButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            clobberButton.setBackground(new Color(184, 207, 229));
            if (index < numGames) {
                gameSum[index] = new Clobber();
                index++;
                if (index == numGames) {
                    gameSumButtonActionPerformed(event);
                } else {
                    questionLabel.setText(openerMessage[3]);
                }
            }
        } else {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    GameModel[] singleGame = new GameModel[]{new Clobber()};
                    GameController controller = new GameController(singleGame);
                    controller.GameController();
                }
            }).start();
        }
    }
    //Allows a saved game to be loaded.

    private void loadButtonActionPerformed(java.awt.event.ActionEvent event) {
        //set to true if loading a game sum
        GameModel[] loadGames = new GameModel[]{};
        boolean twoLoads = false;
        int numLoads = 1;
        if (isGameSum && index == 0) {
            twoLoads = true;
            numLoads = 2;
        }
        if (index == 1) {
            loadGames = gameSum;

        }

        GameController controller = new GameController(loadGames);
        for (int i = 0; i < numLoads; i++) {
            controller.loadGameActionPerformed(event, twoLoads, true);
            index++;
        }
        if (index == numGames) {
            dispose();
        }
    }

    //Exit the game
    private void exitButtonActionPerformed(java.awt.event.ActionEvent event) {
        System.exit(0);
    }

    /**
     * Main method for testing.
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Opener().setVisible(true);
            }
        });
    }
}
